package Interfaz;

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class PanelBotones extends JPanel implements ActionListener
{
	// -----------------------------------------------------------------
    // Constantes
    // -----------------------------------------------------------------
    
	/**
     * Representa los botones
     */
    public static final String BOTON[] = {"UNO", "DOS", "TRES", "CUATRO", "CINCO", "SEIS",
    	"SIETE", "OCHO", "NUEVE", "DIEZ", "ONCE", "DOCE" };
    
    public static final int BOTONES[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    
    public static final String GUARDAR = "GUARDAR";
    
    public static final String ELEGIR = "SELECCIONAR";
    
    final public static int GUARDADOS = 12;

	// -----------------------------------------------------------------
    // Atributos
    // -----------------------------------------------------------------
    
    private InterfazRadio principal;

    private boolean[] guardado = new boolean [GUARDADOS];
    
	// -----------------------------------------------------------------
    // Atributos de interfaz
    // -----------------------------------------------------------------
    
    private JButton botonUno;
    
    private JButton botonDos;
    
    private JButton botonTres;
    
    private JButton botonCuatro;
    
    private JButton botonCinco;
    
    private JButton botonSeis;
    
    private JButton botonSiete;
    
    private JButton botonOcho;
    
    private JButton botonNueve;
    
    private JButton botonDiez;
    
    private JButton botonOnce;
    
    private JButton botonDoce;
    
    private JRadioButton botonGuardar;
    
    private JRadioButton botonElegir;
    
    private ButtonGroup grupo;
    
    // -----------------------------------------------------------------
    // Constructores
    // -----------------------------------------------------------------
    
    public PanelBotones (InterfazRadio r)
    {
    	principal = r;
    	
    	setBorder( new TitledBorder( "Guardar Emisora" ) );
        setLayout( new GridBagLayout( ) );
        
        //Botones
        botonUno = new JButton( "1" );
        botonUno.addActionListener( this );
        botonUno.setActionCommand( BOTON[0]  );
        botonUno.setEnabled(false);
        GridBagConstraints gbc = new GridBagConstraints( );
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonUno, gbc);
        
        botonDos = new JButton( "2" );
        botonDos.addActionListener( this );
        botonDos.setActionCommand( BOTON[1]  );
        botonDos.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonDos, gbc);
        
        botonTres = new JButton( "3" );
        botonTres.addActionListener( this );
        botonTres.setActionCommand( BOTON[2]  );
        botonTres.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonTres, gbc);
        
        botonCuatro = new JButton( "4" );
        botonCuatro.addActionListener( this );
        botonCuatro.setActionCommand( BOTON[3]  );
        botonCuatro.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonCuatro, gbc);
        
        botonCinco = new JButton( "5" );
        botonCinco.addActionListener( this );
        botonCinco.setActionCommand( BOTON[4]  );
        botonCinco.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonCinco, gbc);
        
        botonSeis = new JButton( "6" );
        botonSeis.addActionListener( this );
        botonSeis.setActionCommand( BOTON[5]  );
        botonSeis.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonSeis, gbc);
        
        botonSiete = new JButton( "7" );
        botonSiete.addActionListener( this );
        botonSiete.setActionCommand( BOTON[6]  );
        botonSiete.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonSiete, gbc);
        
        botonOcho = new JButton( "8" );
        botonOcho.addActionListener( this );
        botonOcho.setActionCommand( BOTON[7]  );
        botonOcho.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonOcho, gbc);
        
        botonNueve = new JButton( "9" );
        botonNueve.addActionListener( this );
        botonNueve.setActionCommand( BOTON[8]  );
        botonNueve.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonNueve, gbc);
        
        botonDiez = new JButton( "10" );
        botonDiez.addActionListener( this );
        botonDiez.setActionCommand( BOTON[9]  );
        botonDiez.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonDiez, gbc);
        
        botonOnce = new JButton( "11" );
        botonOnce.addActionListener( this );
        botonOnce.setActionCommand( BOTON[10]  );
        botonOnce.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonOnce, gbc);
        
        botonDoce = new JButton( "12" );
        botonDoce.addActionListener( this );
        botonDoce.setActionCommand( BOTON[11]  );
        botonDoce.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        add( botonDoce, gbc);
        
      //Opcion AM
        botonElegir = new JRadioButton( ELEGIR );
        botonElegir.addActionListener( this );
        botonElegir.setActionCommand( ELEGIR  );
        botonElegir.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = -2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        botonElegir.setSelected(false);
        add( botonElegir, gbc );
        
        //Opcion Guardar
        botonGuardar = new JRadioButton( GUARDAR );
        botonGuardar.addActionListener( this );
        botonGuardar.setActionCommand( GUARDAR );
        botonGuardar.setEnabled(false);
        gbc = new GridBagConstraints( );
        gbc.gridx = -2;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        botonGuardar.setSelected(true);
        add( botonGuardar, gbc );
        
        grupo = new ButtonGroup( );
        grupo.add( botonGuardar );
        grupo.add( botonElegir );
    }
    
    //-----------------------------------------------------------------
    // Métodos
    //-----------------------------------------------------------------
    
    public void habilitarBotones()
    {
    	botonUno.setEnabled(true);
    	botonDos.setEnabled(true);
    	botonTres.setEnabled(true);
    	botonCuatro.setEnabled(true);
    	botonCinco.setEnabled(true);
    	botonSeis.setEnabled(true);
    	botonSiete.setEnabled(true);
    	botonOcho.setEnabled(true);
    	botonNueve.setEnabled(true);
    	botonDiez.setEnabled(true);
    	botonOnce.setEnabled(true);
    	botonDoce.setEnabled(true);
    	botonGuardar.setEnabled(true);
    	botonElegir.setEnabled(true);
    }
    
    public void deshabilitarBotones()
    {
    	botonUno.setEnabled(false);
    	botonDos.setEnabled(false);
    	botonTres.setEnabled(false);
    	botonCuatro.setEnabled(false);
    	botonCinco.setEnabled(false);
    	botonSeis.setEnabled(false);
    	botonSiete.setEnabled(false);
    	botonOcho.setEnabled(false);
    	botonNueve.setEnabled(false);
    	botonDiez.setEnabled(false);
    	botonOnce.setEnabled(false);
    	botonDoce.setEnabled(false);
    	botonGuardar.setEnabled(false);
    	botonElegir.setEnabled(false);
    	botonGuardar.setSelected(true);
    }
    
    

    public void actionPerformed(ActionEvent e) 
    {
    	 String comando = e.getActionCommand( );
    	 
    	for(int i = 0; i < principal.darRadio().SIZE; i++)
    	{
    		String boton = BOTON[i];
    		
    		if(comando.equals(boton))
    		{
    			if(botonGuardar.isSelected())
    			{
    				principal.save(i);
    				guardado[i]=true;
    			}
    			else
    			{
    				if(guardado[i])
    					principal.push(i);
    				else
    					JOptionPane.showMessageDialog( this, "No hay una estación guardada en este botón.", "Notificación", JOptionPane.INFORMATION_MESSAGE );
    			}
    		}
    	}
    }
}
